package org.jeecg.modules.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.engine.entity.Frame;
import org.jeecg.modules.engine.entity.Task;
import org.jeecg.modules.engine.mapper.FrameMapper;
import org.jeecg.modules.engine.mapper.TaskMapper;
import org.jeecg.modules.flow.mapper.FlowMapper;
import org.jeecg.modules.quality.entity.*;
import org.jeecg.modules.quality.mapper.QualityDetailMapper;
import org.jeecg.modules.quality.mapper.QualityTestingMapper;
import org.jeecg.modules.quality.service.IQualityTestingService;
import org.jeecg.modules.reporting.entity.Reporting;
import org.jeecg.modules.reporting.mapper.ReportingMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 质检表
 * @Author: jeecg-boot
 * @Date:   2020-07-23
 * @Version: V1.0
 */
@Service
public class QualityTestingServiceImpl extends ServiceImpl<QualityTestingMapper, QualityTesting> implements IQualityTestingService {

	@Autowired
	private QualityTestingMapper qualityTestingMapper;
	@Autowired
	private QualityDetailMapper qualityDetailMapper;

	@Autowired
	private ReportingMapper reportingMapper;

	@Autowired
	private TaskMapper taskMapper;
	@Autowired
	private FrameMapper frameMapper;

	@Autowired
	private FlowMapper flowMapper;

	@Override
	@Transactional
	public void saveMain(QualityTesting qualityTesting, List<QualityDetail> qualityDetailList) {
		qualityTestingMapper.insert(qualityTesting);
		if(qualityDetailList!=null && qualityDetailList.size()>0) {
			for(QualityDetail entity:qualityDetailList) {
				//外键设置
				entity.setQualityId(qualityTesting.getId());
				qualityDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(QualityTesting qualityTesting,List<QualityDetail> qualityDetailList) {
		qualityTestingMapper.updateById(qualityTesting);

		//1.先删除子表数据
		qualityDetailMapper.deleteByMainId(qualityTesting.getId());

		//2.子表数据重新插入
		if(qualityDetailList!=null && qualityDetailList.size()>0) {
			for(QualityDetail entity:qualityDetailList) {
				//外键设置
				entity.setQualityId(qualityTesting.getId());
				qualityDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		qualityDetailMapper.deleteByMainId(id);
		qualityTestingMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			qualityDetailMapper.deleteByMainId(id.toString());
			qualityTestingMapper.deleteById(id);
		}
	}

	@Override
	public IPage<QualityTestingVo> selectQualityTestingPage(Page<QualityTestingVo> page, QualityTestingVo qualityTestingVo) {
		return baseMapper.selectQualityTestingPage(page,qualityTestingVo);
	}

	@Override
	public IPage<QualityReportingVo> selectQualityTestingByReportIdPage(Page<QualityReportingVo> page, QualityReportingVo qualityReportingVo,String isAll) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		return baseMapper.selectQualityTestingByReportIdPage(page,qualityReportingVo,sysUser.getId(),isAll);
	}

	@Override
	@Transactional
	public void submitQuality(String reportId, Integer qualityCount) {
		Reporting reporting = reportingMapper.selectOne(new LambdaQueryWrapper<Reporting>().eq(Reporting::getId, reportId));
		Task task = taskMapper.selectOne(new LambdaQueryWrapper<Task>().eq(Task::getId, reporting.getTaskId()));
		Frame frame = frameMapper.selectOne(new LambdaQueryWrapper<Frame>().eq(Frame::getId, task.getFramId()));
		QualityTesting qualityTesting = baseMapper.selectOne(new LambdaQueryWrapper<QualityTesting>().eq(QualityTesting::getReportId, reportId));
		if (qualityTesting == null){
			qualityTesting = new QualityTesting();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String currDay = simpleDateFormat.format(new Date());
			int count = baseMapper.selectCount(new LambdaQueryWrapper<QualityTesting>().ge(QualityTesting::getCreateTime, currDay + " 00:00:00").and(p -> p.le(QualityTesting::getCreateTime, currDay + " 23:59:59")));
			String prefix = "ZJ";
			String dateFormat = new SimpleDateFormat("yyyyMMdd").format(new Date());
			String code = prefix+dateFormat+String.format("%04d", count+1);
			qualityTesting.setReportId(reportId);
			qualityTesting.setQualityNo(code);
			baseMapper.insert(qualityTesting);
		}
		QualityDetail qualityDetail = new QualityDetail();
		qualityDetail.setQualityId(qualityTesting.getId());
		qualityDetail.setQualifiedCount(qualityCount);
		qualityDetailMapper.insert(qualityDetail);

		Task updateTask = new Task();
		updateTask.setId(task.getId());
		updateTask.setEligibilityCount(task.getEligibilityCount()+qualityCount);
//		updateTask.setInProcCount(task.getInProcCount()-qualityCount);
		taskMapper.updateById(updateTask);

		Task nextTask = taskMapper.selectOne(new LambdaQueryWrapper<Task>().eq(Task::getFramId, frame.getId()).and(w -> w.eq(Task::getHandleSort, task.getHandleSort() + 1)));
		if (nextTask != null){
			Task updateNextTask = new Task();
			updateNextTask.setId(nextTask.getId());
			updateNextTask.setInProcCount(nextTask.getInProcCount()+qualityCount);
			taskMapper.updateById(updateNextTask);
		}else{ //最后一个工序
			Frame updateFrame = new Frame();
			updateFrame.setId(frame.getId());
			updateFrame.setDoneCount(frame.getDoneCount()+qualityCount);
			if(task.getEligibilityCount()+qualityCount == frame.getCount()){
				updateFrame.setStatus(3);
			}
			frameMapper.updateById(updateFrame);
		}

		Reporting updateReporting = new Reporting();
		updateReporting.setId(reportId);
		updateReporting.setQualifiedCount(reporting.getQualifiedCount()+qualityCount);
		reportingMapper.updateById(updateReporting);
	}

	@Override
	public IPage<MyPhoneQualityVo> selectMyPhoneQualityPage(Page<MyPhoneQualityVo> page, MyPhoneQualityVo myPhoneQualityVo) {
		return baseMapper.selectMyPhoneQualityPage(page,myPhoneQualityVo);
	}

	@Override
	@Transactional
	public boolean returnQuality(String id){
		ReturnQualityVo returnQualityVo = baseMapper.selectVoById(id);
		qualityDetailMapper.delete(new LambdaQueryWrapper<QualityDetail>().eq(QualityDetail::getId,id));
		if (returnQualityVo.getCurrentEligibilityCount()-returnQualityVo.getQualifiedCount()==0){
			baseMapper.delete(new LambdaQueryWrapper<QualityTesting>().eq(QualityTesting::getId,returnQualityVo.getQualityDetailId()));
		}
		Reporting reporting = new Reporting();
		reporting.setId(returnQualityVo.getReportingId());
		reporting.setQualifiedCount(returnQualityVo.getReportQualityCount() - returnQualityVo.getQualifiedCount());
		reportingMapper.updateById(reporting);
		Task currentTask = new Task();
		currentTask.setId(returnQualityVo.getCurrentTaskId());
		currentTask.setEligibilityCount(returnQualityVo.getCurrentEligibilityCount()-returnQualityVo.getQualifiedCount());
		taskMapper.updateById(currentTask);
		Task nextTask = new Task();
		nextTask.setId(returnQualityVo.getNextTaskId());
		nextTask.setInProcCount(returnQualityVo.getNextTaskInProcCount()-returnQualityVo.getQualifiedCount());
		taskMapper.updateById(nextTask);
		return true;
	}

	@Override
	public QualityReportingVo selectQualityTestingByReportId(String reportId) {
		return baseMapper.selectQualityTestingByReportId(reportId);
	}

}
